package com.humngyang.com.hmy.study.concurrency.myannoations.aqs;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

public class CyclicBarrierExample2 {
    private static Logger log = LoggerFactory.getLogger(CyclicBarrierExample2.class);
    private static final CyclicBarrier cb = new CyclicBarrier(5,() ->{
        log.info("before continue run");
    });

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Thread.sleep(2000);
        for (int i = 0; i < 10; i++) {
            final int count = i;
            String name = Thread.currentThread().getName();
            executorService.execute(() -> {
                try {
                    cyclicBarrier2(count);
                } catch (Exception e) {
                   log.warn("exception",e);
                }
            });
        }
    }

    public static void cyclicBarrier2(int count) throws Exception {
        Thread.sleep(2000);
        log.info("is ready {}", count);
        cb.await(2000, TimeUnit.MICROSECONDS);
        log.info("is continue {}", count);
    }
}
